/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PesquisaSecaoView.java
 *
 * Created on 19/05/2011, 00:15:11
 */

package br.com.comprasPublicas.view.pesquisa;

import br.com.comprasPublicas.dao.DAOGenerico;
import br.com.comprasPublicas.entity.Secao;
import br.com.comprasPublicas.view.cadastro.PedidoView;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author helsen
 */
public class PesquisaSecaoView extends javax.swing.JDialog {

    /** Creates new form PesquisaSecaoView */
    public PesquisaSecaoView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        tblSecaoDepartamento.setSelectionMode(0);

        preencher_tblSecaoDepartamento();
        tblSecaoDepartamento.requestFocus();

        //Método utilizado para centralizar o JFrame na tela
        Dimension tela = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((tela.width - this.getSize().width) / 2, (tela.height - this.getSize().height) / 2);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblSecaoDepartamento = new javax.swing.JTable();
        btnSelecionar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(br.com.comprasPublicas.compraspublicas.ComprasPublicasApp.class).getContext().getResourceMap(PesquisaSecaoView.class);
        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblSecaoDepartamento.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Secao", "Cod. Secao", "Departamento"
            }
        ));
        tblSecaoDepartamento.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tblSecaoDepartamento.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tblSecaoDepartamento.setName("tblSecaoDepartamento"); // NOI18N
        tblSecaoDepartamento.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        tblSecaoDepartamento.getTableHeader().setResizingAllowed(false);
        tblSecaoDepartamento.getTableHeader().setReorderingAllowed(false);
        tblSecaoDepartamento.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tblSecaoDepartamentoFocusLost(evt);
            }
        });
        jScrollPane1.setViewportView(tblSecaoDepartamento);

        btnSelecionar.setFont(resourceMap.getFont("btnSelecionar.font")); // NOI18N
        btnSelecionar.setMnemonic('s');
        btnSelecionar.setText(resourceMap.getString("btnSelecionar.text")); // NOI18N
        btnSelecionar.setName("btnSelecionar"); // NOI18N
        btnSelecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelecionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(btnSelecionar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSelecionar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblSecaoDepartamentoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tblSecaoDepartamentoFocusLost

}//GEN-LAST:event_tblSecaoDepartamentoFocusLost

    private void btnSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelecionarActionPerformed
        if (tblSecaoDepartamento.getSelectedRow()==-1){
         JOptionPane.showMessageDialog(null, "Nenhum item Selecionado");
        }
        else{
            String selecao = tblSecaoDepartamento.getModel().getValueAt(tblSecaoDepartamento.getSelectedRow(), 1).toString();
            PedidoView.sec = selecao;
            secoes = null;
            dispose();
        }
}//GEN-LAST:event_btnSelecionarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PesquisaSecaoView dialog = new PesquisaSecaoView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSelecionar;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblSecaoDepartamento;
    // End of variables declaration//GEN-END:variables

   public void preencher_tblSecaoDepartamento()
    {
        try
        {
            /*
            DefaultTableModel modelo_Dtm = new DefaultTableModel(null, new String [] {"Secao", "Departamento"})
            {
                @Override
                public boolean isCellEditable(int row, int col)
                {
                    return false;
                }
            };
             *
             */

            tblSecaoDepartamento.getColumnModel().getColumn(0).setPreferredWidth(100);
            tblSecaoDepartamento.getColumnModel().getColumn(1).setPreferredWidth(5);
            tblSecaoDepartamento.getColumnModel().getColumn(2).setPreferredWidth(100);



            DefaultTableModel modelo_tbl = (DefaultTableModel) tblSecaoDepartamento.getModel();



            modelo_tbl.setNumRows(0);

            Secao secao = new Secao();
            DAOGenerico obj_dao_generico = new DAOGenerico(secao);

            List<Secao> lista = new ArrayList();
            lista = obj_dao_generico.executa_query("order_by_sec");
            int tamanho_lista = lista.size();

            for(int i = 0; i < tamanho_lista;i++)
            {
                secao = lista.get(i);
                modelo_tbl.addRow(new Object[]{secao.getSecao(),
                                            secao.getId_secao(),
                                            secao.getDepartamento().getDepartamento(),
                                            });
            }
        }
        catch(Exception erro)
        {
            JOptionPane.showMessageDialog(null, "Não foi possível preencher tabela "+erro);
        }
    }

    private List<Secao> secoes;
}
